<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="分錄編號" prop="entryCode">
        <el-input
          v-model="queryParams.entryCode"
          placeholder="請輸入分錄編號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="會計科目(借)" prop="accountSubject">
        <el-cascader
          filterable
          :options="accountSubjectArr"
          :props="{
            expandTrigger: 'hover',
            value: 'dictValue',
            label: 'name',
            children: 'children',
          }"
          @change="queryASHandleChange"
          clearable
        ></el-cascader>
      </el-form-item>

      <el-form-item label="會計科目(貸)" prop="accountSubjectD">
        <el-cascader
          filterable
          :options="accountSubjectArr"
          :props="{
            expandTrigger: 'hover',
            value: 'dictValue',
            label: 'name',
            children: 'children',
          }"
          @change="queryASHandleChangeD"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="摘要" prop="summary">
        <el-input
          v-model="queryParams.summary"
          placeholder="請輸入摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="金額" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="請輸入金額"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="提交時間" prop="submitTime">
        <el-date-picker
          clearable
          v-model="queryParams.submitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="請選擇提交時間"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="賬戶ID" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="請輸入賬戶ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:entry:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:entry:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:entry:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:entry:export']"
          >導出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="entryList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="分錄類型" align="center" prop="entryType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.account_entry_type"
            :value="scope.row.entryType"
          />
        </template>
      </el-table-column>

      <el-table-column
        width="180"
        label="分錄編號"
        align="center"
        prop="entryCode"
      />
      <el-table-column
        label="科目分類(借)"
        align="center"
        width="120"
        prop="jAccountSubjectType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.as_zero_0000"
            :value="scope.row.jAccountSubjectType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="一級科目(借)"
        width="120"
        align="center"
        prop="jAccountSubjectOne"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict_one" :value="scope.row.jAccountSubjectOne" />
        </template>
      </el-table-column>

      <el-table-column
        label="二級科目(借)"
        width="120"
        align="center"
        prop="jAccountSubjectTwo"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict_two" :value="scope.row.jAccountSubjectTwo" />
        </template>
      </el-table-column>

      <el-table-column
        label="科目分類(貸)"
        width="120"
        align="center"
        prop="dAccountSubjectType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.as_zero_0000"
            :value="scope.row.dAccountSubjectType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="一級科目(貸)"
        width="120"
        align="center"
        prop="dAccountSubjectOne"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict_one" :value="scope.row.dAccountSubjectOne" />
        </template>
      </el-table-column>
      <el-table-column
        label="二級科目(貸)"
        width="120"
        align="center"
        prop="dAccountSubjectTwo"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict_two" :value="scope.row.dAccountSubjectTwo" />
        </template>
      </el-table-column>
      <el-table-column label="摘要" align="center" prop="summary" />
      <el-table-column label="金額" align="center" prop="amount" />
      <el-table-column
        label="提交時間"
        align="center"
        prop="submitTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="會計賬期"
        align="center"
        prop="accountingPeriod"
      />
      <!-- <el-table-column
        width="110"
        label="關聯單據ID"
        align="center"
        prop="relationId"
      /> -->
      <el-table-column label="分錄狀態" align="center" prop="entryStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.account_entry_status"
            :value="scope.row.entryStatus"
          />
        </template>
      </el-table-column>

      <el-table-column label="備註" align="center" prop="remark" />
      <el-table-column
        label="操作"
        width="160"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="scope.row.entryStatus != 10"
            v-hasPermi="['biz:entry:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.entryStatus != 10"
            v-hasPermi="['biz:entry:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
          <div>
            <el-button
              style="padding: 5px"
              size="mini"
              type="primary"
              :disabled="scope.row.entryStatus != 15"
              v-hasPermi="['biz:entry:confirm']"
              @click="Confirm(scope.row)"
              >確認</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改會計記賬分錄对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分錄編號" prop="entryCode">
          <el-input v-model="form.entryCode" placeholder="請輸入分錄編號" />
        </el-form-item>
        <el-form-item label="一級科目" prop="jAccountSubjectOne">
          <el-input
            v-model="form.jAccountSubjectOne"
            placeholder="請輸入一級科目"
          />
        </el-form-item>
        <el-form-item label="二級科目" prop="jAccountSubjectTwo">
          <el-input
            v-model="form.jAccountSubjectTwo"
            placeholder="請輸入二級科目"
          />
        </el-form-item>
        <el-form-item label="一級科目" prop="dAccountSubjectOne">
          <el-input
            v-model="form.dAccountSubjectOne"
            placeholder="請輸入一級科目"
          />
        </el-form-item>
        <el-form-item label="二級科目" prop="dAccountSubjectTwo">
          <el-input
            v-model="form.dAccountSubjectTwo"
            placeholder="請輸入二級科目"
          />
        </el-form-item>
        <el-form-item label="摘要" prop="summary">
          <el-input v-model="form.summary" placeholder="請輸入摘要" />
        </el-form-item>
        <el-form-item label="金額" prop="amount">
          <el-input v-model="form.amount" placeholder="請輸入金額" />
        </el-form-item>
        <el-form-item label="提交時間" prop="submitTime">
          <el-date-picker
            clearable
            v-model="form.submitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇提交時間"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="賬戶ID" prop="bankId">
          <el-input v-model="form.bankId" placeholder="請輸入賬戶ID" />
        </el-form-item>
        <el-form-item label="備註" prop="remark">
          <el-input v-model="form.remark" placeholder="請輸入備註" />
        </el-form-item>
        <el-form-item label="${comment}" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="請輸入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="open_confirm" width="500px" append-to-body>
      <el-form
        ref="form_confirm"
        :model="form_confirm"
        :rules="rules_confirm"
        label-width="120px"
      >
        <el-form-item label="會計科目(借)" prop="accountSubjectArrJ">
          <el-cascader
            style="width: 100%"
            filterable
            @input="$forceUpdate()"
            v-model="form_confirm.accountSubjectArrJ"
            :options="accountSubjectArr"
            :props="{
              expandTrigger: 'hover',
              value: 'dictValue',
              label: 'name',
              children: 'children',
            }"
            @change="queryASHandleChangeJ_confirm"
            clearable
          ></el-cascader>
        </el-form-item>

        <el-form-item label="會計科目(貸)" prop="accountSubjectArrD">
          <el-cascader
            filterable
            style="width: 100%"
            @input="$forceUpdate()"
            v-model="form_confirm.accountSubjectArrD"
            :options="accountSubjectArr"
            :props="{
              expandTrigger: 'hover',
              value: 'dictValue',
              label: 'name',
              children: 'children',
            }"
            @change="queryASHandleChangeD_confirm"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" @click="submitForm_confirm">確 定</el-button>
        <el-button @click="cancel_confirm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAccountSubjectTree,
  listEntry,
  getEntry,
  postEntryConfirm,
  delEntry,
  addEntry,
  updateEntry,
} from "@/api/biz/entry";

export default {
  name: "Entry",
  dicts: [
    "account_entry_type",
    "as_zero_0000",
    "as_one_1000",
    "as_one_2000",
    "as_one_3000",
    "as_one_4000",
    "as_one_5000",
    "as_one_6000",
    "as_two_1002",
    "as_two_2241",
    "account_entry_status",
    "customer_level",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 會計記賬分錄表格數据
      entryList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entryType: null,
        entryCode: null,
        jAccountSubjectType: null,
        jAccountSubjectOne: null,
        jAccountSubjectTwo: null,
        dAccountSubjectType: null,
        dAccountSubjectOne: null,
        dAccountSubjectTwo: null,
        summary: null,
        amount: null,
        submitTime: null,
        bankId: null,
        relationId: null,
        entryStatus: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      // 表单校验
      rules: {
        entryType: [
          { required: true, message: "分錄類型不能為空", trigger: "change" },
        ],
        jAccountSubjectType: [
          { required: true, message: "科目分類不能為空", trigger: "change" },
        ],
        jAccountSubjectOne: [
          { required: true, message: "一級科目不能為空", trigger: "blur" },
        ],
        amount: [{ required: true, message: "金額不能為空", trigger: "blur" }],
      },
      dict_one: [],
      dict_two: [],
      accountSubjectArr: [],
      open_confirm: false,
      form_confirm: {
        accountSubjectArrJ: [],
        accountSubjectArrD: [],
      },
      rules_confirm: {
        accountSubjectArrJ: [
          { required: true, message: "科目分類不能為空", trigger: "change" },
        ],
        accountSubjectArrD: [
          { required: true, message: "科目分類不能為空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getAccountSubjectTree();
  },
  methods: {
    /** 查询收入記錄列表 */
    getAccountSubjectTree() {
      this.loading = true;
      getAccountSubjectTree().then((response) => {
        this.accountSubjectArr = response.data;
      });
    },
    queryASHandleChange(subject) {
      console.log(subject);
      if (subject.length === 0) {
        this.queryParams.jAccountSubjectType = "";
        this.queryParams.jAccountSubjectOne = "";
        this.queryParams.jAccountSubjectTwo = "";
      }
      if (subject.length === 1) {
        this.queryParams.jAccountSubjectType = subject[0];
      }
      if (subject.length === 2) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.jAccountSubjectOne = subject[1];
      }
      if (subject.length === 3) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.jAccountSubjectOne = subject[1];
        this.queryParams.jAccountSubjectTwo = subject[2];
      }
    },
    queryASHandleChangeD(subject) {
      console.log(subject);
      if (subject.length === 0) {
        this.queryParams.dAccountSubjectType = "";
        this.queryParams.dAccountSubjectOne = "";
        this.queryParams.dAccountSubjectTwo = "";
      }
      if (subject.length === 1) {
        this.queryParams.dAccountSubjectType = subject[0];
      }
      if (subject.length === 2) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.dAccountSubjectOne = subject[1];
      }
      if (subject.length === 3) {
        this.queryParams.dAccountSubjectType = subject[0];
        this.queryParams.dAccountSubjectOne = subject[1];
        this.queryParams.dAccountSubjectTwo = subject[2];
      }
    },
    queryASHandleChangeJ_confirm(subject) {},
    queryASHandleChangeD_confirm(subject) {},
    /** 查询會計記賬分錄列表 */
    getList() {
      this.loading = true;
      listEntry(this.queryParams).then((response) => {
        this.dict_one = [
          ...this.dict.type.as_one_1000,
          ...this.dict.type.as_one_2000,
          ...this.dict.type.as_one_3000,
          ...this.dict.type.as_one_4000,
          ...this.dict.type.as_one_5000,
          ...this.dict.type.as_one_6000,
        ];
        this.dict_two = [
          ...this.dict.type.as_two_1002,
          ...this.dict.type.as_two_2241,
        ];
        this.entryList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entryType: null,
        entryCode: null,
        jAccountSubjectType: null,
        jAccountSubjectOne: null,
        jAccountSubjectTwo: null,
        dAccountSubjectType: null,
        dAccountSubjectOne: null,
        dAccountSubjectTwo: null,
        summary: null,
        amount: null,
        submitTime: null,
        bankId: null,
        relationId: null,
        entryStatus: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({ path: "/entry/index/add" });
      // this.open = true;
      // this.title = "添加會計記賬分錄";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.$router.push({ path: "/entry/index/edit", query: { id: id } });
      // getEntry(id).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改會計記賬分錄";
      // });
    },
    handleDetail(row) {
      const id = row.id || this.ids;
      this.$router.push({ path: "/entry/index/detail", query: { id: id } });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEntry(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEntry(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除會計記賬分錄編號為"' + ids + '"的數据项？')
        .then(function () {
          return delEntry(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    Confirm(row) {
      console.log(22222);
      console.log(row);
      this.form_confirm.id = "";
      this.form_confirm.accountSubjectArrJ = [];
      this.form_confirm.accountSubjectArrD = [];
      if (row.jAccountSubjectType) {
        this.form_confirm.accountSubjectArrJ.push(row.jAccountSubjectType);
      }
      if (row.jAccountSubjectOne && row.jAccountSubjectOne != "") {
        this.form_confirm.accountSubjectArrJ.push(row.jAccountSubjectOne);
      }
      if (row.jAccountSubjectTwo && row.jAccountSubjectTwo != "") {
        this.form_confirm.accountSubjectArrJ.push(row.jAccountSubjectTwo);
      }

      if (row.dAccountSubjectType) {
        this.form_confirm.accountSubjectArrD.push(row.dAccountSubjectType);
      }
      if (row.dAccountSubjectOne && row.dAccountSubjectOne != "") {
        this.form_confirm.accountSubjectArrD.push(row.dAccountSubjectOne);
      }
      if (row.dAccountSubjectTwo && row.dAccountSubjectTwo != "") {
        this.form_confirm.accountSubjectArrD.push(row.dAccountSubjectTwo);
      }
      // this.form_confirm.accountSubjectArrJ = [];
      // this.form_confirm.accountSubjectArrD = [];

      // form_confirm.accountSubjectArrJ

      const ids = row.id || this.ids;
      this.open_confirm = true;
      this.form_confirm.id = ids;
    },
    cancel_confirm() {
      this.open_confirm = false;
    },
    submitForm_confirm() {
      console.log(this.form_confirm);
      let that = this;
      this.loading = true;
      this.$refs["form_confirm"].validate((valid) => {
        if (valid) {
          let postData = {
            id: this.form_confirm.id,
            jAccountSubjectType: this.form_confirm.accountSubjectArrJ[0],
            jAccountSubjectOne: this.form_confirm.accountSubjectArrJ[1],
            jAccountSubjectTwo: this.form_confirm.accountSubjectArrJ[2],
            dAccountSubjectType: this.form_confirm.accountSubjectArrD[0],
            dAccountSubjectOne: this.form_confirm.accountSubjectArrD[1],
            dAccountSubjectTwo: this.form_confirm.accountSubjectArrD[2],
          };
          postEntryConfirm(postData).then((response) => {
            console.log(response);
            this.loading = false;
            that.$modal.msgSuccess("確認成功");
            that.cancel_confirm();
            that.getList();
          });
          // this.$modal
          //   .confirm('是否確認編號為"' + this.form_confirm.id + '"的數据项？')
          //   .then(function () {
          //     postEntryConfirm(postData).then((response) => {
          //       that.$modal.msgSuccess("確認成功");
          //       that.cancel_confirm();
          //       that.getList();
          //     });
          //   })
          //   .catch(() => {});
        }
      });
    },
    /** 導出按钮操作 */
    handleExport() {
      this.download(
        "biz/entry/export",
        {
          ...this.queryParams,
        },
        `entry_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
